package com.lovo.stock.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lovo.stock.bean.CustomerBean;
import com.lovo.stock.bean.PageInfo;
import com.lovo.stock.constant.MessageCode;
import com.lovo.stock.service.ICustomerService;
import com.lovo.stock.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    ICustomerService customerService;

    /**
     * 分页查询客户信息
     * @param pageInfo
     * @return
     */
    @RequestMapping("/queryCustomerByPage")
    public ResponseResult<IPage<CustomerBean>> queryCustomerByPage(@RequestBody PageInfo<CustomerBean> pageInfo){
        ResponseResult<IPage<CustomerBean>> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(customerService.queryCustomerByPage(pageInfo));
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 根据id查询客户信息
     * @param id
     * @return
     */
    @RequestMapping("/queryCustomerInfoById")
    public ResponseResult<CustomerBean> queryCustomerInfoById(int id){
        ResponseResult<CustomerBean> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(customerService.queryCustomerInfoById(id));
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 查询所有客户
     * @return
     */
    @RequestMapping("/queryAllCustomer")
    public ResponseResult<List<CustomerBean>> queryAllCustomer(){
        ResponseResult<List<CustomerBean>> responseResult=new ResponseResult<>();
        try {
            responseResult.setData(customerService.queryAllCustomer());
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
        }
        return responseResult;
    }

    /**
     * 更新客户信息
     * @param customerBean
     * @return
     */
    @RequestMapping("/updateCustomer")
    public ResponseResult updateCustomer(@RequestBody CustomerBean customerBean){
        ResponseResult responseResult=new ResponseResult<>();
        try {
            customerService.updateCustomer(customerBean);
            return responseResult;
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
            return responseResult;
        }
    }

    /**
     * 新增客户
     * @param customerBean
     * @return
     */
    @RequestMapping("/addCustomer")
    public ResponseResult addCustomer(@RequestBody CustomerBean customerBean){
        ResponseResult responseResult=new ResponseResult<>();
        try {
            customerService.addCustomer(customerBean);
            return responseResult;
        }catch (Exception e){
            e.printStackTrace();
            responseResult.setMessage(MessageCode.ERROR.message);
            responseResult.setCode(MessageCode.ERROR.code);
            return responseResult;
        }
    }
}
